﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerBase.Service.BaseDispatch;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using ServerBase.Service;
using ServerBase.Config;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using System;
using ServerPublic.Data;
using ServerGame.GameObject;
using static UtilPublic.Serialization.Jsh;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;
using static ServerGame.Manager.Efun;
using System.Linq;

namespace ServerGame.Manager
{
    public partial class DispatcherMsg
    {
        [ApiDpPro((short)EProtocolId.C2G_EQUIP_GET, "拾取")]
        public short OnEquipGet()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnEquipGet();

            if (player.IsBusy)
            {
                Rsp.Result = EProtocolResult.你正忙着呢; return Send(Rsp);
            }
            Rsp.Uid = Req.Uid;
            var room = player.Room;
            if (!room.EquipHas(Req.Uid))
            {
                Rsp.MessageFail("你附近没有这样东西。"); return Send(Rsp);
            }
            player.EquipGet(room, Req.Uid, out var equip);
            MessageVision($"$N捡起一{equip.Cfg.Unit}{equip.Cfg.Name}。", player);

            Send(Rsp);

            room.RoomThing();
            return 0;
        }
        [ApiDpPro((short)EProtocolId.C2G_EQUIP_INFOROOM, "查看")]
        public short OnEquipInfoRoom()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnEquipInfoRoom();

            var room = player.Room;
            if (!room.DictEquip.TryGetValue(Req.Uid, out var equip))
            {
                Rsp.MessageFail("你附近没有这样东西。"); return Send(Rsp);
            }
            equip.EquipInfoAll(ref Rsp.Info);

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_EQUIP_INFOINV, "查看")]
        public short OnEquipInfoInv()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnEquipInfoInv();

            if (!player.DaCommon.DictEquip.TryGetValue(Req.Uid, out var equip))
            {
                Rsp.MessageFail("你身上没有这样东西。"); return Send(Rsp);
            }
            equip.EquipInfoAll(ref Rsp.Info);

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_EQUIP_DROP, "丢弃")]
        public short OnEquipDrop()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnEquipDrop();

            if (player.IsBusy)
            {
                Rsp.Result = EProtocolResult.你正忙着呢; return Send(Rsp);
            }
            if (!player.DaCommon.DictEquip.TryGetValue(Req.Uid, out var equip))
            {
                Rsp.MessageFail("你身上没有这样东西。"); return Send(Rsp);
            }
            player.EquipDrop(Req.Uid);
            MessageVision($"$N丢下#HIG#一#NOR#$u$n。", player, equip);

            player.EquipFinish();

            player.EquipInfo(ref Rsp.DictEquip);
            player.WearInfo(ref Rsp.DictWear);

            Send(Rsp);

            player.Room.RoomThing();

            return 0;
        }
        [ApiDpPro((short)EProtocolId.C2G_EQUIP_WEAR, "装备")]
        public short OnEquipWear()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnEquipWear();

            if (!player.DaCommon.DictEquip.TryGetValue(Req.Uid, out var equip))
            {
                Rsp.MessageFail("你身上没有这样东西。"); return Send(Rsp);
            }
            //if (player.DaCommon.DictWear.ContainsKey((EEquipType)equip.Cfg.EquipType))
            //{
            //    Rsp.MessageFail("你已经装备了同类型的装备了。"); return Send(Rsp); return;
            //}
            player.EquipUnwear((EEquipType)equip.Cfg.EquipType, true);
            player.EquipWear(equip, true);

            player.EquipFinish();

            player.EquipInfo(ref Rsp.DictEquip);
            player.WearInfo(ref Rsp.DictWear);

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_EQUIP_CHANGE, "装备")]
        public short OnEquipChange()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnEquipChange();

            player.EquipUnwear((EEquipType)Req.Type, true);
            if (player.DaCommon.DictEquip.TryGetValue(Req.Uid, out var equip))
            {
                player.EquipWear(equip, true);
            }
            
            player.EquipFinish();

            player.EquipInfo(ref Rsp.DictEquip);
            player.WearInfo(ref Rsp.DictWear);

            return Send(Rsp);
        }
        [ApiDpPro((short)EProtocolId.C2G_EQUIP_UNWEAR, "卸下")]
        public short OnEquipUnwear()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnEquipUnwear();

            if (!player.DaCommon.DictEquip.TryGetValue(Req.Uid, out var equip))
            {
                Rsp.MessageFail("你身上没有这样东西。"); return Send(Rsp);
            }
            if (!player.DaCommon.DictWear.ContainsValue(equip.Uid))
            {
                Rsp.MessageFail("你并没有装备这样东西。。"); return Send(Rsp);
            }
            player.EquipUnwear((EEquipType)equip.Cfg.EquipType, true);
            
            player.EquipFinish();

            player.EquipInfo(ref Rsp.DictEquip);
            player.WearInfo(ref Rsp.DictWear);

            return Send(Rsp);
        }

    }
}
